<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class foreground_good_index_action_service extends foreground_default_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        //var_dump(__METHOD__); exit();
        
        $result = array('status' => 0, 'message' => '', 'data' => array());
        
        $request_data = $_GET;
        //var_dump($request_data); exit();
        
        //模拟数据
        //$cat_id   = 0;
        //$cat_id   = 3;    
        $cat_id = init_user_data_key($request_data, 'cat_id', 'int');
        //$page_num = 1;
        $page_num = init_user_data_key($request_data, 'page_num', 'int');
        //var_dump($page_num); exit();
        //$item_count = 2;
        //$item_count = 10;
        $item_count = 20;
        $page_range = 5;
    	
    	$cache_handler = wwy_resource_container::get_cache_handler();
    	
    	//分类
    	$model_foreground_good_category = wwy_resource_container::get_model_foreground_good_category();
    	//var_dump($model_foreground_good_category); exit();
    	if($cat_id < 0){
    	    $cat_id = 0;
    	}
    	if($cat_id > 0){
    	    //$good_category_info = $model_foreground_good_category->find($cat_id);
    	    $good_category_info = $cache_handler->call(array($model_foreground_good_category, 'find'), array($cat_id));
    	    //var_dump($good_category_info); exit();        	    
    	    if(empty($good_category_info)){

    	        $result['status']  = -4;
    	        $result['message'] = '该分类不存在';
    	    }
    	}    	    	    	      	
        
    	if($result['status'] >= 0){           
            $model_foreground_good = wwy_resource_container::get_model_foreground_good();
            $where = array('status' => 1);
            $order = array('`order` asc', 'id desc');
            $options = array('item_count' => $item_count, 'page_num' => $page_num, 'page_range' => $page_range);
            
            //$self_children_id_list = $model_foreground_good_category->get_self_children_id_list($direct_children_cat['id']);
            $self_children_id_list = $cache_handler->call(array($model_foreground_good_category, 'get_self_children_id_list'), array($cat_id));
            $where['cat_ids'] = $self_children_id_list;
            
            //$good_list = $model_foreground_good->get_list($where, $order, $options);
            $good_list = $cache_handler->call(array($model_foreground_good, 'get_list'), array($where, $order, $options));
            
            self::add_view_data('good_list', $good_list['list']);
            self::add_view_data('page_data', $good_list['page_data']);
    
            //$index_cats = $model_foreground_good_category->get_index_cats($cat_id);
            $index_cats = $cache_handler->call(array($model_foreground_good_category, 'get_index_cats'), array($cat_id));
            //var_dump($index_cats); exit();
            self::add_view_data('index_cats', $index_cats);
    	}
        
        self::set_view_status($result['status']);
        self::set_view_message($result['message']);        

        $page_title = '商品列表';
        if(!empty($index_cats['index_cat_names'])){
            $page_title = implode(' - ', $index_cats['index_cat_names']) . ' - ' . $page_title;
        }
        self::add_view_data('page_title', $page_title);
    }
}